iT邦幫忙

2025 iThome 鐵人賽

DAY 17
0
生成式 AI

30 天打造你的 AI Agent:LangChain × n8n 實戰系列 第 17

自動摘要 Agent:幫你整理新聞或文章

  • 分享至 

  • xImage
  •  

昨天我們學會了 ReAct Agent,能處理多步推理。
今天,我們要做一個更實用的工具 —— 自動摘要 Agent
它能幫你把長文章、新聞、甚至研究報告,快速濃縮成精簡重點。


一、概念

  • 輸入 → 一段長文章或新聞
  • Agent 思考 → 理解內容、找出關鍵資訊
  • 輸出 → 重點摘要(條列式)

應用場景:

  • 快速閱讀新聞
  • 整理研究資料
  • 幫會議記錄做重點整理

二、實作範例

from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain_google_genai import ChatGoogleGenerativeAI
import os

# -------------------------------
# 1. API Key 設定
# -------------------------------
os.environ["GOOGLE_API_KEY"] = "你的 Gemini API Key"

# -------------------------------
# 2. 初始化 LLM
# -------------------------------
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0)

# -------------------------------
# 3. 建立摘要模板
# -------------------------------
template = """請將以下文章整理成 3 點重點:
文章內容:
{text}"""

prompt = PromptTemplate(input_variables=["text"], template=template)

# -------------------------------
# 4. 建立 Chain
# -------------------------------
summary_chain = LLMChain(llm=llm, prompt=prompt)

# -------------------------------
# 5. 測試文章
# -------------------------------
article = """
LangChain 是一個幫助開發者快速構建 LLM 應用的框架,
可以串接工具、記憶上下文,甚至建立問答型 Agent。
"""

summary = summary_chain.run(article)
print("【自動摘要】\n", summary)

📌 範例輸出為:

【自動摘要】
 以下是將文章整理成的 3 點重點:

1.  **LangChain 是一個幫助開發者快速構建 LLM 應用的框架。**        
2.  **它能串接工具、記憶上下文。**
3.  **甚至能建立問答型 Agent。**

三、今日小結

  • Chain + PromptTemplate 就能做摘要工具
  • 可以快速整理 新聞 / 文章 / 長文本
  • 是未來進一步做 知識管理、文件分析 的基礎

上一篇
進階推理:ReAct 架構與多步驟思考
下一篇
資料分析 Agent:讀取 CSV 並產生結論
系列文
30 天打造你的 AI Agent:LangChain × n8n 實戰21
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言